{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 179,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import os\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 180,
   "metadata": {},
   "outputs": [],
   "source": [
    "file_names = os.listdir('digits/digits/trainingDigits')\n",
    "label = '5_70.txt'.split('_')[0]\n",
    "txt = np.loadtxt('digits/digits/trainingDigits/5_70.txt', dtype='str')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 181,
   "metadata": {},
   "outputs": [],
   "source": [
    "txt = np.loadtxt('digits/digits/trainingDigits/5_70.txt', dtype='str')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 182,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1024"
      ]
     },
     "execution_count": 182,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(''.join(txt))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 183,
   "metadata": {},
   "outputs": [],
   "source": [
    "path = 'digits/trainingDigits'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 184,
   "metadata": {},
   "outputs": [],
   "source": [
    "def getFileDF(path):\n",
    "    file_names = os.listdir(path)\n",
    "    labels = []\n",
    "    txts = []\n",
    "    for file_name in file_names:\n",
    "        label = file_name.split('_')[0]\n",
    "        file_path = path + '/' + file_name\n",
    "\n",
    "        txt = np.loadtxt(file_path, dtype='str')\n",
    "        labels.append(label)\n",
    "        txts.append(''.join(txt))\n",
    "    \n",
    "    return pd.DataFrame({\n",
    "        'label': labels,\n",
    "        'txt': txts\n",
    "    })"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 185,
   "metadata": {},
   "outputs": [],
   "source": [
    "train = getFileDF('digits/digits/trainingDigits')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 186,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>label</th>\n",
       "      <th>txt</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>7</td>\n",
       "      <td>0000000000000000011111000000000000000000000011...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>9</td>\n",
       "      <td>0000000000000111110000000000000000000000000011...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>5</td>\n",
       "      <td>0000000000000000011111000000000000000000000000...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>5</td>\n",
       "      <td>0000000000011111111000000000000000000000011111...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>6</td>\n",
       "      <td>0000000000000111100000000000000000000000000001...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>941</th>\n",
       "      <td>0</td>\n",
       "      <td>0000000000000001100000000000000000000000000111...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>942</th>\n",
       "      <td>3</td>\n",
       "      <td>0000000000000000111111000000000000000000000000...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>943</th>\n",
       "      <td>8</td>\n",
       "      <td>0000000000000011110000000000000000000000001111...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>944</th>\n",
       "      <td>5</td>\n",
       "      <td>0000000000000000011111100000000000000000000011...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>945</th>\n",
       "      <td>7</td>\n",
       "      <td>0000000000000000111111100000000000000000011111...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>946 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    label                                                txt\n",
       "0       7  0000000000000000011111000000000000000000000011...\n",
       "1       9  0000000000000111110000000000000000000000000011...\n",
       "2       5  0000000000000000011111000000000000000000000000...\n",
       "3       5  0000000000011111111000000000000000000000011111...\n",
       "4       6  0000000000000111100000000000000000000000000001...\n",
       "..    ...                                                ...\n",
       "941     0  0000000000000001100000000000000000000000000111...\n",
       "942     3  0000000000000000111111000000000000000000000000...\n",
       "943     8  0000000000000011110000000000000000000000001111...\n",
       "944     5  0000000000000000011111100000000000000000000011...\n",
       "945     7  0000000000000000111111100000000000000000011111...\n",
       "\n",
       "[946 rows x 2 columns]"
      ]
     },
     "execution_count": 186,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "getFileDF('digits/digits/testDigits')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 187,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>label</th>\n",
       "      <th>txt</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>8</td>\n",
       "      <td>0000000000000001111000000000000000000000000001...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3</td>\n",
       "      <td>0000000001111111111110000000000000000000111111...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>7</td>\n",
       "      <td>0000000000000000111110000000000000000000000000...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>0000000000000000001111110000000000000000000000...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>9</td>\n",
       "      <td>0000000000000000000011111110000000000000000000...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1929</th>\n",
       "      <td>6</td>\n",
       "      <td>0000000000000100000000000000000000000000000011...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1930</th>\n",
       "      <td>5</td>\n",
       "      <td>0000000000111011111111110000000000000000011111...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1931</th>\n",
       "      <td>2</td>\n",
       "      <td>0000000000011111000000000000000000000000011111...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1932</th>\n",
       "      <td>7</td>\n",
       "      <td>0000000011111100000000011000000000000001111111...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1933</th>\n",
       "      <td>5</td>\n",
       "      <td>0000000000000000110000000000000000000000000111...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1934 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     label                                                txt\n",
       "0        8  0000000000000001111000000000000000000000000001...\n",
       "1        3  0000000001111111111110000000000000000000111111...\n",
       "2        7  0000000000000000111110000000000000000000000000...\n",
       "3        1  0000000000000000001111110000000000000000000000...\n",
       "4        9  0000000000000000000011111110000000000000000000...\n",
       "...    ...                                                ...\n",
       "1929     6  0000000000000100000000000000000000000000000011...\n",
       "1930     5  0000000000111011111111110000000000000000011111...\n",
       "1931     2  0000000000011111000000000000000000000000011111...\n",
       "1932     7  0000000011111100000000011000000000000001111111...\n",
       "1933     5  0000000000000000110000000000000000000000000111...\n",
       "\n",
       "[1934 rows x 2 columns]"
      ]
     },
     "execution_count": 187,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 199,
   "metadata": {},
   "outputs": [],
   "source": [
    "def hammingDF(df,str2):\n",
    "    dists = []\n",
    "    for (i,row) in df.iterrows():\n",
    "        dist = hamming(row[0],str2)\n",
    "        dists.append(dist)\n",
    "    return pd.DataFrame({\n",
    "        'dist':dists,\n",
    "        'label':df['label']\n",
    "    })\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 189,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<zip at 0x7fce7bfe3e80>"
      ]
     },
     "execution_count": 189,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "zip('str1','str2')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 190,
   "metadata": {},
   "outputs": [],
   "source": [
    "def hamm(str1,str2):\n",
    "    return sum ([a !=b for a,b in zip(str1,str2)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 191,
   "metadata": {},
   "outputs": [],
   "source": [
    "def knn(inX, df, k):\n",
    "    df_l = hammingDF(df, inX)\n",
    "    df_rank = df_l.sort_values(axis=0, by='dist', ascending=True)\n",
    "    return df_rank.iloc[:k].value_counts('label').index[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 192,
   "metadata": {},
   "outputs": [],
   "source": [
    "test = getFileDF('digits/digits/testDigits')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 193,
   "metadata": {},
   "outputs": [],
   "source": [
    "def DigitsTest(train, test, k):\n",
    "    f_arr = []\n",
    "    for i, row in test.iterrows():\n",
    "        inX = row[0]\n",
    "        f_arr.append(knn(inX, train, k))\n",
    "    \n",
    "    df_res = pd.DataFrame({\n",
    "        'f_label': f_arr\n",
    "    })\n",
    "    return (df_res['f_label'] == test['label']).mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 200,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 200,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "DigitsTest(train,test, 3)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "d44d76ef8cbbc4331cecfe2e59228ac31ebb71026289858a116838be7168b60b"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
